#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int Max = 0;
        int zero = k;
        for (int i = 0, a = 0; i < nums.size();)
        {
            while (i < nums.size() && (nums[i] == 1 || zero != 0))
            {
                if (nums[i] == 0)
                {
                    zero--;
                }
                i++;
            }
            Max = max(Max, i - a);
            while (a < nums.size() && zero == 0)
            {
                if (nums[a++] == 0)
                    zero++;
            }
        }
        return Max;
    }
};